package com.wzh.test.kmp;

/**
 * 暴力匹配字符串
 */
public class Demo1 {
    public static void main(String[] args) {
        //有一个字符串""
        String str1 = "abcabaabcabac";
        String str2 = "abaa";

        System.out.println(getIndexOf(str1,str2));

    }

    public static int getIndexOf(String str1,String str2){
        char[] chars1 = str1.toCharArray();
        char[] chars2 = str2.toCharArray();

        int i = 0;
        int j = 0;
        int str1Length = chars1.length;
        int str2Length = chars2.length;

        while (i < str1Length && j < str2Length){
            if(chars1[i] == chars2[j]){
                i++;
                j++;
            }else{
                i = i - j + 1;
                j = 0;
            }
        }

        if(j == str2Length){
            return i - j;
        }else{
            return -1;
        }
    }
}
